package calculator;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Calculator extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField monitor;
	private JButton but;
	private double result=0;
	private String preop="+";
	private boolean numbool=false;
	private boolean multiop=false;
	private boolean isNew=true;
	
	public Calculator() {
		setTitle("计算器");
		setSize(305, 410);
		setLocation(200, 300);
		
		Font font = new Font("微软雅黑", Font.TRUETYPE_FONT, 18);
		
		JPanel pane=new JPanel();
		pane.setLayout(new BorderLayout());
		add(pane,BorderLayout.NORTH);
		
		monitor = new JTextField("0");
		monitor.setHorizontalAlignment(JTextField.RIGHT);
		monitor.setEditable(false);
		monitor.setDisabledTextColor(Color.black);
		monitor.setFont(font);
		monitor.setBorder(BorderFactory.createLoweredBevelBorder());
		pane.add(monitor, BorderLayout.CENTER);
		
		but=new JButton("清除");
		but.addActionListener(this);
		but.setBackground(Color.DARK_GRAY);
		but.setFont(font);
		
		pane.add(but, BorderLayout.EAST);
		
		JPanel panel1=new JPanel();
		panel1.setLayout(new GridLayout(4, 4,3,1));
		add(panel1, BorderLayout.CENTER);
		
		String[] str={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
		for(int i=0;i<str.length;i++){
			but=new JButton(str[i]);
			but.setBackground(Color.BLACK);
			but.setBorder(BorderFactory.createLoweredBevelBorder());
			but.setFont(font);
			but.addActionListener(this);
			panel1.add(but);
		}
		
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String btCom=e.getActionCommand();
		System.out.println(btCom);
		if(btCom.equals("清除")){
			monitor.setText("");
			result=0;
			preop="+";
		}else if("0123456789.".indexOf(btCom)>=0){
			showNum(btCom);
		}else{
			compute(btCom);
		}
		
	}
	
	
	private void compute(String btCom) {
		isNew=true;
		String value=monitor.getText();
//		preop=btCom;
		
		if(multiop){preop=btCom;return;}
		if(numbool){monitor.setText("");return;}
		if(preop.equals("/")&value.equals("0")){
			monitor.setText("0");
			numbool=true;
			return;
		}
		
		Double num =new Double(monitor.getText());
		BigDecimal bd= new BigDecimal(result);
		
		switch (preop) {
		case "+":
			result+=num;
			break;
		case "-":
//			bd=bd.subtract(num);
//			result=bd.doubleValue();
			break;
		case "*":
//			bd=bd.multiply(num);
//			result=bd.doubleValue();
			break;
		case "/":
//			bd=bd.divide(num);
//			result=bd.doubleValue();
			break;
		case "=":
//			bd=bd.add(num);
//			result=bd.doubleValue();
		default:
			break;
		}
		String str=String.valueOf(result);
		if(str.endsWith(".0")) str=str.substring(0, str.length()-2);
		monitor.setText(str);
		preop=btCom;
		multiop=true;
	}

	private void showNum(String btCom) {
		multiop=false;
		String originNum=monitor.getText();
		
		if(numbool){monitor.setText("");return;}
		
		if(preop.equals("/")&&originNum.equals("0")&&btCom!="."){
			monitor.setText("");
			numbool=true;
			return;
		}
		
		if(isNew){
			originNum="0";
			isNew=false;
		}
		
		if(originNum.equals("0")){
			if(btCom.equals(".")){
				monitor.setText("0.");
			}else{
				monitor.setText(btCom);
			}
			return;
		}
		if(originNum.indexOf(".")>=0&&btCom.equals(".")){
			return;
		}
		monitor.setText(originNum+btCom);
		
	}

	public static void main(String[] args) throws InvocationTargetException, InterruptedException {
		SwingUtilities.invokeAndWait(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new Calculator();
			}
		});
	}
}
